package com.baosu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baosu.domain.*;
import com.baosu.service.ArticleService;
import com.baosu.mapper.ArticleMapper;
import com.baosu.service.ArticleTagService;
import com.baosu.service.CategoryService;
import com.baosu.service.TagService;
import com.baosu.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService{

    @Autowired
    private ArticleTagService articleTagService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Result add(ArticleDto articleDto) {
        articleDto.setCreateTime(new Date());
        articleDto.setUpdateTime(new Date());
        this.save(articleDto);

        ArrayList<ArticleTag> articleTags = new ArrayList<>();

        //保存文章和 新标签的一对多关系
        List<String> tagsNew = articleDto.getTagsNew();
        if(tagsNew != null){
            for (String s : tagsNew) {
                //保存新标签
                Tag tag = new Tag();
                tag.setName(s);
                tagService.save(tag);
                //和文章绑定关系
                ArticleTag articleTag = new ArticleTag();
                articleTag.setTagId(tag.getId());
                articleTag.setArticleId(articleDto.getId());
                articleTags.add(articleTag);
            }
        }
        //保存文章和 已存在标签的一对多关系
        List<Long> tags = articleDto.getTags();
        for (Long tagid : tags) {
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(articleDto.getId());
            articleTag.setTagId(tagid);
            articleTags.add(articleTag);
        }
        articleTagService.saveBatch(articleTags);
        return Result.ok(null).message("添加文章成功");
    }

    @Override
    public Result deleteArticleAndTag(List<Long> ids) {


        for (Long ArticleId : ids) {
            LambdaQueryWrapper<ArticleTag> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ArticleTag::getArticleId, ArticleId);
            articleTagService.remove(wrapper);
        }
        this.removeByIds(ids);
        return Result.ok(null).message("删除文章成功");
    }

    @Override
    public Result updateArticle(ArticleDto articleDto) {
        articleDto.setUpdateTime(new Date());
        this.updateById(articleDto);

        //先删除中间表关系
        Long articleId = articleDto.getId();
        LambdaQueryWrapper<ArticleTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ArticleTag::getArticleId,articleId);
        articleTagService.remove(wrapper);

        ArrayList<ArticleTag> articleTags = new ArrayList<>();

        //保存文章和 新标签的一对多关系
        List<String> tagsNew = articleDto.getTagsNew();
        if(tagsNew != null){
            for (String s : tagsNew) {
                //保存新标签
                Tag tag = new Tag();
                tag.setName(s);
                tagService.save(tag);
                //和文章绑定关系
                ArticleTag articleTag = new ArticleTag();
                articleTag.setTagId(tag.getId());
                articleTag.setArticleId(articleDto.getId());
                articleTags.add(articleTag);
            }
        }
        //保存文章和 已存在标签的一对多关系
        List<Long> tags = articleDto.getTags();
        for (Long tagid : tags) {
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(articleDto.getId());
            articleTag.setTagId(tagid);
            articleTags.add(articleTag);
        }
        articleTagService.saveBatch(articleTags);
        return Result.ok(null).message("更新文章成功");
    }

    @Override
    public Result selectById(Long id) {
        ArticleDto vo = new ArticleDto();
        //赋值基本属性
        Article article = this.getById(id);
        BeanUtils.copyProperties(article, vo);

        //赋值分类名称
        Category category = categoryService.getById(article.getCategoryId());
        if (category != null) {
            vo.setCategoryName(category.getName());
        }

        //根据ID查询出标签集合
        LambdaQueryWrapper<ArticleTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleTag::getArticleId, id);
        List<ArticleTag> list = articleTagService.list(wrapper);
        //赋值标签集合
        ArrayList<Long> arr = new ArrayList<>();
        for (ArticleTag articleTag : list) {
            arr.add(articleTag.getTagId());
        }
        vo.setTags(arr);
        //返回 VO
        return Result.ok(vo);
    }

    @Override
    public Result pageTagList(String query, int pagenum, int size) {
        Page<Article> page = new Page<>(pagenum,size);
        List<Article> list = articleMapper.getArticleAndCategoryName(page,query);
        for (Article article : list) {
            String content = article.getContent();
            String prefix = StringUtil.setArticlePrefix(content);
            article.setPrefix(prefix);
        }
        page.setRecords(list);
        return Result.ok(page).message("查询文章成功");
    }

    @Override
    public Result updateCount(Long id) {
        Article article = this.getById(id);
        article.setCount(article.getCount() + 1L);
        this.updateById(article);
        return Result.ok(null);
    }

    @Override
    public Result hotArticleList() {
        List<Article> list = articleMapper.hotArticleList();
        return Result.ok(list);
    }
}




